package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.article.config.ApArticleThreadLocalUtil;
import com.heima.article.service.IApLoadArticleBehaviorService;
import com.heima.common.constants.BehaviorContstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class ApLoadArticleBehaviorServiceImpl implements IApLoadArticleBehaviorService {

    @Resource
    private CacheService cacheService;

    @Override
    public ResponseResult loadArticle(ArticleInfoDto dto) {
        boolean islike = false;
        boolean isunlike = false;
        boolean iscollection = false;
        boolean isfollow = false;

        System.out.println("文章id" + dto.getArticleId());

        ApUser user = ApArticleThreadLocalUtil.getUser();
        System.out.println("user = " + user);

        if (user != null) {
            //喜欢
            Object o = cacheService.hGet(BehaviorContstants.LIKE_BEHAVIOR + dto.getArticleId(), user.getId().toString());
            if (o != null) {
                islike = true;
            }
            //不喜欢
            Object o1 = cacheService.hGet(BehaviorContstants.UN_LIKE_BEHAVIOR + dto.getArticleId(), user.getId().toString());
            if (o1 != null) {
                isunlike = true;
            }
            //收藏
            Object o2 = cacheService.hGet(BehaviorContstants.COLLECTION_BEHAVIOR + user.getId().toString(), String.valueOf(dto.getArticleId()));
            if (o2 != null) {
                iscollection = true;
            }
            //关注
            Double aDouble = cacheService.zScore(BehaviorContstants.APUSER_FOLLOW_RELATION + user.getId().toString(), String.valueOf(dto.getAuthorId()));
            if (aDouble != null) {
                isfollow = true;
            }
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("isfollow", isfollow);
        resultMap.put("islike", islike);
        resultMap.put("isunlike", isunlike);
        resultMap.put("iscollection", iscollection);

        return ResponseResult.okResult(resultMap);
    }
}
